#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
pair<string, ll>p[1005];
#define x first
#define y second
ll zhuan(string s)
{
    ll sum = 0;
    for (ll h = 0;h < s.size();h++)
    {
        for (ll k = h + 1;k < s.size();k++)
        {
            if (s[k] < s[h])sum++;
        }
    }return sum;
}
bool com(pair<string, ll>& a, pair<string, ll>& b)
{
    if (a.y != b.y)return a.y < b.y;
    else if (a.x.size() != b.x.size())return a.x.size() < b.x.size();
    else return a.x < b.x;
}
int main()
{
    int n;cin >> n;
    for (int i = 1;i <= n;i++)cin >> p[i].x;
    for (int i = 1;i <= n;i++)p[i].y = zhuan(p[i].x);
    sort(p + 1, p + 1 + n, com);
    for (int i = 1;i <= n;i++)cout << p[i].x << '\n';
    return 0;
}